package com.mew.lobby.handler.mail;


import com.mew.common.dc.redis.UserProfileRedisService;
import com.mew.common.entity.user.Profile;
import com.mew.common.exception.ResponseCode;
import com.mew.common.rest.IntMessage;
import com.mew.common.rest.bo.MewSession;
import com.mew.lobby.annotation.LobbyHandler;
import com.mew.lobby.message.response.MailReceiveResponse;
import com.mew.lobby.net.LobbyHandlerMapCmdType;
import com.mew.lobby.net.MewHandler;
import com.mew.lobby.provide.MailFeignService;
import com.zebra.remoting.protocol.ZebraCommand;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 *领取邮件
 * @author dylin
 * @create 2021/4/6
 */
@Service
@Slf4j
@LobbyHandler(LobbyHandlerMapCmdType.MailReceive)
public class MailReceiveHandler implements MewHandler<IntMessage> {
    @Autowired
    private UserProfileRedisService profileRedisService;
    @Autowired
    private MailFeignService mailFeignService;


    @Override
    public void handle(ZebraCommand<IntMessage> zebraCommand, MewSession session) {
        long userId=zebraCommand.getUserId();
        int mailId=zebraCommand.getRequest().getI();
        int code=mailFeignService.receiveAward(userId,mailId);
        if(code == ResponseCode.Success.code()){
            Profile profile=profileRedisService.getRedisProfileById(userId);
            zebraCommand.sendMessage(MailReceiveResponse.builder()
                    .mailId(mailId)
                    .goldNumber(profile.getGoldNumber())
                    .jewelNumber(profile.getJewelNumber())
                    .couponNumber(0l)
                    .build());
        }else{
            zebraCommand.sendFailureCode(code);
        }
    }
}
